package com.yc.mvc;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/9
 * Email haijunzhou@hnit.edu.cn
 */
// web.xml
public class WebConfig implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 加载 Spring 配置
        AnnotationConfigWebApplicationContext context =
                new AnnotationConfigWebApplicationContext();
        context.register(MvcConfig.class);
        // 注册转发器Servlet
//        DispatcherServlet servlet = new DispatcherServlet(context);
//        ServletRegistration.Dynamic registration =
//                servletContext.addServlet("app", servlet);
//        // 设置WEB服务器在启动时加载 DispatcherServlet
//        registration.setLoadOnStartup(1);
//        // 转发全部请求，必须配置响应地址为：/
//        registration.addMapping("/");

        MyDispatcherServlet servlet = new MyDispatcherServlet(context);
                ServletRegistration.Dynamic registration =
                servletContext.addServlet("app", servlet);
        // 设置WEB服务器在启动时加载 DispatcherServlet
        registration.setLoadOnStartup(1);
        // 转发全部请求，必须配置响应地址为：/
        registration.addMapping("/");
    }
}
